/**********************************************************
 * AdminLoginAction.java
 *
 * Created by Nishan
 * Last modified Date: Aug 1, 2008 9:18:42 AM by nishan
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.AdminServiceDelegator;
import com.printeverywhere.dto.Admin;
import com.printeverywhere.util.MessageConstants;
import static com.printeverywhere.util.AttributeConstants.*;

/**
 * The Class AdminLoginAction.
 */
public class AdminLoginAction extends ActionSupport implements ServletRequestAware {
	
	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(AdminLoginAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The request. */
	private HttpServletRequest request;

	/**
	 * Authenticates Admin. and set it to Session for attribute Admin
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@SuppressWarnings("unchecked")
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		Admin admin =null;
		try{
			admin = AdminServiceDelegator.getAdminByName(username);
		}catch (Exception e) {
			logger.error("execute()", e); //$NON-NLS-1$
			addActionError(getText((MessageConstants.ERROR_ADMIN_LOGIN_INVALID)).intern());
			if (logger.isDebugEnabled()) {
				logger.debug("execute() - end"); //$NON-NLS-1$
			}
            return INPUT;
		}
        if(admin != null && admin.getPassword().equals(password)){
            request.getSession().setAttribute(ADMIN, admin);
			if (logger.isDebugEnabled()) {
				logger.debug("execute() - end"); //$NON-NLS-1$
			}
            return SUCCESS;
        }else{
              addActionError(getText((MessageConstants.ERROR_ADMIN_LOGIN_INVALID)).intern());
			if (logger.isDebugEnabled()) {
				logger.debug("execute() - end"); //$NON-NLS-1$
			}
              return INPUT;
        }
	}

	public String logout() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("logout() - start"); //$NON-NLS-1$
		}

		request.getSession().removeAttribute(ADMIN);
		request.getSession().invalidate();

		if (logger.isDebugEnabled()) {
			logger.debug("logout() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Show admin login page.
	 *
	 * @return the string
	 */
	public String showLogin(){
		if(request.getSession().getAttribute(ADMIN) != null)
			return SUCCESS;
		else
			return LOGIN;
	}

	/** The username. */
	private String username;

	/** The password. */
	private String password;

	/**
	 * Gets the username.
	 *
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Sets the username.
	 *
	 * @param username the new username
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * Gets the password.
	 *
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Sets the password.
	 *
	 * @param password the new password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Sets the servlet request.
	 * 
	 * @param arg0 the arg0
	 * 
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest arg0) {
		request= arg0;
	}
}
